<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
$return=array();
$return['type_xuanxiang']=array (
  0 => '本地',
  1 => 'FTP服务器',
  2 => '阿里云OSS',
  3 => '七牛云存储',
  4 => '腾讯云存储 ',
  5 => '自定义资源站',
);
$return['image_ext_xuanxiang']=array (
  'gif' => 'gif',
  'png' => 'png',
  'jpg' => 'jpg',
  'jpeg' => 'jpeg',
  'ico' => 'ico',
);
$return['audio_ext_xuanxiang']=array (
  'mp3' => 'mp3',
  'wav' => 'wav',
  'wma' => 'wma',
  'ogg' => 'ogg',
);
$return['video_ext_xuanxiang']=array (
  'wmv' => 'wmv',
  'avi' => 'avi',
  'mp4' => 'mp4',
  'flv' => 'flv',
  'rmvb' => 'rmvb',
  'mov' => 'mov',
);
$return['other_ext_xuanxiang']=array (
  'txt' => 'txt',
  'zip' => 'zip',
  'rar' => 'rar',
  'xls' => 'xls',
  'xlsx' => 'xlsx',
  'csv' => 'csv',
  'pem' => 'pem',
  'key' =>'key',
  'apk' =>'apk',
  'doc' =>'doc',
  'docx' =>'docx',
  'ppt' =>'ppt',
  'pptx' =>'pptx',
  'pdf' =>'pdf',
  'cer' =>'cer',
  'pft' =>'pft',
  'pfx' =>'pfx',
    'ipa' =>'ipa',
);
$return['image_weis']=array (
    '1' => '左上角',
    '2' => '右上角',
    '3' => '居中位置',
    '4' => '左下角',
    '5' => '右下角',
);
if($_RQ['op']=='get'){
	$value=core_setting('upload');
	$return['item']=$value?$value:array();
	$htmls=array(
		array('type'=>'edit',
			'post'=>'post',
			'desc'=>'附件设置',
			'edit'=>array(
				array('type'=>'tip','title'=>'开启远程附件后，请注意如果你网站是HTTPS访问，而远程附件是HTTP会导致附件无法读取，包括模板数据！所有尽量保持运程附件网址是HTTPS'),
				array('type'=>'radio','title'=>'附件服务器类型','name'=>'type','help'=>'设置附件服务器类型','options'=>'type_xuanxiang','help'=>'!注意：更改附件服务器前，请先将当前站点附件资源('.ROOT_AT.'目录下文件),拷贝到即将要使用的附件服务器相应（<red>自定义域名/URL根目录</red>）目录，以免出现附件资源无法访问的问题。<a href="http://s.jinyunweb.com/manage/index.php?p=core&action=article.show&nosession=1&id=114" target="_blank">附件无效？看教程</a>'),
				array('type'=>'switch','title'=>'启用ssl连接','name'=>'ftp_ssl','help'=>'','show'=>array('type',1)),
				array('type'=>'string','title'=>'FTP服务器地址','name'=>'ftp_ip','must'=>true,'help'=>'','show'=>array('type',1)),
				array('type'=>'string','title'=>'FTP服务器端口','name'=>'ftp_dk','must'=>true,'help'=>'','show'=>array('type',1)),
				array('type'=>'string','title'=>'FTP帐号','name'=>'ftp_user','must'=>true,'help'=>'','show'=>array('type',1)),
				array('type'=>'password','title'=>'FTP密码','name'=>'ftp_psw','must'=>true,'help'=>'','show'=>array('type',1)),
				array('type'=>'switch','title'=>'被动模式(pasv)连接','name'=>'ftp_pasv','help'=>'','show'=>array('type',1)),
				array('type'=>'string','title'=>'远程附件目录','name'=>'ftp_dir','help'=>'','show'=>array('type',1)),
				array('type'=>'string','title'=>'访问URL','name'=>'ftp_url','help'=>'','show'=>array('type',1)),
				array('type'=>'string','title'=>'FTP传输超时时间','name'=>'ftp_time','help'=>'单位秒，0为服务器默认','show'=>array('type',1)),
				array('type'=>'string','title'=>'Access Key ID','name'=>'ali_accessid','must'=>true,'help'=>'https://ram.console.aliyun.com/manage/ak AccessKey 管理','show'=>array('type',2)),
				array('type'=>'string','title'=>'Access Key Secret','name'=>'ali_accesskey','must'=>true,'help'=>'https://ram.console.aliyun.com/manage/ak AccessKey 管理','show'=>array('type',2)),
				array('type'=>'string','title'=>'Bucket','name'=>'ali_bucket','must'=>true,'help'=>'https://oss.console.aliyun.com/ 创建bucket获取','show'=>array('type',2)),
				array('type'=>'string','title'=>'EndPoint(外网/内网)','name'=>'ali_endpoint','help'=>'(如果想用内网，请咨询阿里云客服是否支持)在概览-访问域名中可以查看,不带http,如‘oss-cn-shenzhen.aliyuncs.com’','show'=>array('type',2)),
				array('type'=>'string','title'=>'自定义URL(Bucket 域名)','name'=>'ali_url','help'=>'（跟上面的EndPoint是对应关系，要么都是内网，要么都是外网）不填写使用默认(不带http)，比如：外网访问（外网Bucket 域名）/ECS 的经典网络访问（内网）/ ECS 的 VPC 网络访问（内网）','show'=>array('type',2)),
				array('type'=>'string','title'=>'Accesskey','name'=>'qiniu_accesskey','must'=>true,'help'=>'在七牛后台-个人面板中可查看','show'=>array('type',3)),
				array('type'=>'string','title'=>'Secretkey','name'=>'qiniu_secretkey','must'=>true,'help'=>'在七牛后台-个人面板中可查看','show'=>array('type',3)),
				array('type'=>'string','title'=>'Bucket','name'=>'qiniu_bucket','must'=>true,'help'=>'填空间名称','show'=>array('type',3)),
				array('type'=>'string','title'=>'URL','name'=>'qiniu_url','help'=>'已绑定域名填绑定域名，如未绑定域名填测试域名，不要带http','show'=>array('type',3)),
				array('type'=>'string','title'=>'APPID','name'=>'tx_appid','must'=>true,'help'=>'前往：腾讯云API密钥管理（https://console.cloud.tencent.com/cam/capi），创建一个密钥！','show'=>array('type',4)),
				array('type'=>'string','title'=>'SecretID','name'=>'tx_secretid','must'=>true,'help'=>'','show'=>array('type',4)),
				array('type'=>'string','title'=>'SecretKEY','name'=>'tx_secretkey','must'=>true,'help'=>'','show'=>array('type',4)),
				array('type'=>'string','title'=>'Bucket','name'=>'tx_bucket','must'=>true,'help'=>'前往：腾讯云COS（https://console.cloud.tencent.com/cos5/bucket），创建一个存储桶（1、权限选择-公有读私有写，2、防盗链设置：开启，添加你的域名）！存储桶的名称，“-”后面的忽略，例如:xxxcos-1257095888，填xxxcos即可！','show'=>array('type',4)),
				array('type'=>'string','title'=>'bucket所属区域','name'=>'tx_region','help'=>'例如：ap-beijing','show'=>array('type',4),'options'=>'tx_regions'),
				array('type'=>'string','title'=>'自定义域名','name'=>'tx_url','help'=>'','show'=>array('type',4),'help'=>'不能带http://或https://;前往：腾讯云COS（https://console.cloud.tencent.com/cos5/bucket），创建一个存储桶--配置管理--访问域名'),
				array('type'=>'string','title'=>'资源站访问地址','name'=>'jya_url','must'=>true,'help'=>'下载资源站站点文件，创建资源站点，将站点文件（<a href="javascript:;" ng-click="access_json(\'/system.attachment/down/\')">点击下载</a>）解压获得“resource.php”文件放至站点根目录并给予执行权限即可使用。此处配置资源站的访问地址（如：http://resource.cs.com）。','show'=>array('type',5)),
				array('type'=>'string','title'=>'资源站请求地址','name'=>'jya_request','must'=>true,'help'=>'本地址用于系统对接上传下载数据，可以可访问地址用同一个。也可以另外设置一个专用地址（例如，如果附件服务器和主服务器可以通过内网访问，则此处填写内网访问地址，可以提升请求速度）','show'=>array('type',5)),
				array('type'=>'switch','title'=>'启用图片压缩','name'=>'thumb','help'=>''),
				array('type'=>'string','title'=>'压缩后图片宽度','name'=>'width','help'=>'单位：px;一般手机图片最大不超过750，PC端最大1920左右'),
				array('type'=>'string','title'=>'图片大小限制','name'=>'image','help'=>'单位M,<red>上传失败也有可能是服务器php.ini的配置，允许上传附件设置的太小了，默认设置都是2M</red>'),
				array('type'=>'checkbox','title'=>'图片支持后缀设置','name'=>'image_ext','options'=>'image_ext_xuanxiang','help'=>'设置允许上传的附件类型，以分号隔开，示例：;jpg;png;jpeg;gif;'),
                array('type'=>'switch','title'=>'启用图片水印','name'=>'image_yin','help'=>''),
                array('type'=>'radio','title'=>'图片水印位置','name'=>'image_wei','options'=>'image_weis','show'=>array('image_yin'=>1),'help'=>''),
                array('type'=>'image','title'=>'图片水印LOGO','name'=>'image_yinimg','show'=>array('image_yin'=>1),'help'=>''),
                array('type'=>'unitstring','unit'=>'%','title'=>'图片水印透明度','name'=>'image_yinrate','help'=>''),
				array('type'=>'string','title'=>'音频大小限制','name'=>'audio','help'=>'单位M,<red>上传失败也有可能是服务器php.ini的配置，允许上传附件设置的太小了，默认设置都是2M</red>'),
				array('type'=>'checkbox','title'=>'音频支持后缀设置','name'=>'audio_ext','options'=>'audio_ext_xuanxiang','help'=>'设置允许上传的附件类型，以分号隔开，示例：;wma;mp3;ogg;'),
				array('type'=>'string','title'=>'视频大小限制','name'=>'video','help'=>'单位M,<red>上传失败也有可能是服务器php.ini的配置，允许上传附件设置的太小了，默认设置都是2M</red>'),
				array('type'=>'checkbox','title'=>'视频附件支持格式','name'=>'video_ext','options'=>'video_ext_xuanxiang','help'=>'（<red>建议只支持MP4格式和mov格式（苹果手机的），因为其他格式视频会存在不同浏览器（苹果，安卓）兼容性问题！</red>）设置允许上传的附件类型，以分号隔开，示例：avi;wmv;rmvb;mp4;mov;'),
				array('type'=>'string','title'=>'其他类型文件大小限制','name'=>'other','help'=>'单位M'),
				array('type'=>'checkbox','title'=>'其他类型附件支持格式','name'=>'other_ext','options'=>'other_ext_xuanxiang','help'=>'设置允许上传的附件类型，以分号隔开，示例：;txt;zip;csv;pem;'),
			)
		),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if($postdata['type']==1 && (!$postdata['ftp_ip'] || !$postdata['ftp_dk'] || !$postdata['ftp_user'] || !$postdata['ftp_psw'])){
		exi('FTP服务器相关参数必填！','error');
	}
	if($postdata['type']==2 && (!$postdata['ali_accessid'] || !$postdata['ali_accesskey'] || !$postdata['ali_bucket'] || !$postdata['ali_endpoint'])){
		exi('阿里云OSS相关参数必填！','error');
	}
	if($postdata['type']==3 && (!$postdata['qiniu_accesskey'] || !$postdata['qiniu_bucket'] || !$postdata['qiniu_secretkey'])){
		exi('七牛云存储相关参数必填！','error');
	}
	if($postdata['type']==4 && (!$postdata['tx_appid'] || !$postdata['tx_bucket'] || !$postdata['tx_secretkey'])){
		exi('腾讯云相关参数必填！','error');
	}
	if($postdata['type']==5 && (!$postdata['jya_url'] || !$postdata['jya_request'])){
		exi('自定义资源站相关参数必填！','error');
	}
	core_setting('upload',$postdata);	
	$attachment=array('type'=>intval($postdata['type']));
	if($attachment['type']==0){
		$attachment['attachment_url']='../'.RESOURCE.'/';
	}elseif($attachment['type']==1){
		$attachment['attachment_url']=$postdata['ftp_url'].'/';
	}elseif($attachment['type']==2){
	    if($postdata['ali_url']){
	        $attachment['attachment_url']='https://'.$postdata['ali_url'].'/';
	    }else{
	        $attachment['attachment_url']='https://'.$postdata['ali_bucket'].'.'.$postdata['ali_endpoint'].'/';
	    }
	}elseif($attachment['type']==3){
		$attachment['attachment_url']=HTTP.$postdata['qiniu_url'].'/';
	}elseif($attachment['type']==4){
		$attachment['attachment_url']='https://'.$postdata['tx_url'].'/';
	}elseif($attachment['type']==5){
		//自定义资源站
		$attachment['attachment_url']=$postdata['jya_url'].'/'.RESOURCE.'/';
	}
	core_setting('attachment',$attachment);
	
	exi('附件设置设置成功！','','./index.php?plugin=core&action=system.attachment');
}
if($_RQ['op']=='down'){
	$download_url=download_url('core/system.attachment/download');
	exi('生成资源站接口文件成功！点击确定下载！','','',$download_url);
}
if($_RQ['op']=='download'){
	//生成并下载资源站文件包。对接云平台生成资源文件。
	$zip=cloud_request('cloud.get_resource',array('resource'=>RESOURCE,'base64_key'=>BASE64_KEY));
	$source_key=substr($zip,0,32);
	core_setting('source_key',array('source_key'=>$source_key));
	$zip=substr($zip,32);
	$filename='jinyunweb';
	header("Content-type:application/zip");
	header("Content-Disposition:attachment; filename=".$filename.".zip");
	echo $zip;
	exit();
}